package com.tencent.wxcloudrun.manager;

import java.util.Objects;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import com.alibaba.fastjson2.JSONObject;
import com.tencent.wxcloudrun.domain.TUser;
import com.tencent.wxcloudrun.domain.TUserAddress;
import com.tencent.wxcloudrun.exception.BusinessException;
import com.tencent.wxcloudrun.service.TUserAddressService;
import com.tencent.wxcloudrun.service.TUserService;
import com.tencent.wxcloudrun.util.RedisBusiUtil;
import com.tencent.wxcloudrun.vo.request.user.*;
import com.tencent.wxcloudrun.vo.response.user.*;

import lombok.extern.slf4j.Slf4j;

/**
 * @Author ZhaoZiYin
 * @Date 2025/4/22
 * @Description 用户管理端
 */
@Service
@Slf4j
public class UserManager {

    @Resource
    private TUserService userService;

    @Resource
    private TUserAddressService userAddressService;

    /**
     * @Author: ZhaoZiYin
     * @Date: 2025/4/23
     * @Params: [reqVo]
     * @Return: UserQueryResVo
     * @Description: 用户查询
     */
    @Transactional(rollbackFor = Exception.class)
    public UserQueryResVo query(UserQueryReqVo reqVo) {
        UserQueryResVo resVo = new UserQueryResVo();
        // 查询用户信息
        TUser user = userService.query(reqVo.getUserId(), reqVo.getWxOpenId());
        if (Objects.isNull(user) && StringUtils.hasLength(reqVo.getWxOpenId())) {
            // 如果用户不存在则新增默认用户
            log.info("用户查询-新增默认用户-用户信息=[id={}, wxOpenId={}]", reqVo.getUserId(), reqVo.getWxOpenId());
            user = userService.addDefUser(reqVo.getWxOpenId());
            RedisBusiUtil.saveUserInfo(user);
        }
        // 构建出参
        if (Objects.nonNull(user)) {
            resVo.build(user);
        }
        return resVo;
    }

    /**
     * @Author: ZhaoZiYin
     * @Date: 2025/4/22
     * @Params: [reqVo]
     * @Return: UserAddResVo
     * @Description: 用户新增
     */
    @Transactional(rollbackFor = Exception.class)
    public UserAddResVo add(UserAddReqVo reqVo) {
        log.info("用户新增-服务处理开始-入参={}", JSONObject.toJSONString(reqVo));
        UserAddResVo resVo = new UserAddResVo();
        TUser user = reqVo.build();
        userService.save(user);
        RedisBusiUtil.saveUserInfo(user);
        resVo.setUserId(user.getId());
        log.info("用户新增-服务处理成功-用户信息=[id={},name={},wxOpenId={}]", user.getId(), user.getName(), user.getWxOpenId());
        return resVo;
    }

    /**
     * @Author: ZhaoZiYin
     * @Date: 2025/4/22
     * @Params: [reqVo]
     * @Return: UserUpdateResVo
     * @Description: 用户更新
     */
    @Transactional(rollbackFor = Exception.class)
    public UserUpdateResVo update(UserUpdateReqVo reqVo) {
        log.info("用户更新-服务处理开始-入参={}", JSONObject.toJSONString(reqVo));

        TUser user = userService.getById(reqVo.getUserId());
        if (Objects.isNull(user)) {
            log.error("用户更新-用户不存在-用户信息=[id={}]", reqVo.getUserId());
            throw new BusinessException("用户不存在");
        }
        reqVo.build(user);
        userService.updateById(user);
        RedisBusiUtil.saveUserInfo(user);

        log.info("用户更新-服务处理成功");
        return null;
    }

    /**
     * @Author: ZhaoZiYin
     * @Date: 2025/4/23
     * @Params: [reqVo]
     * @Return: UserQueryAddressResVo
     * @Description: 查询用户收货地址
     */
    public UserQueryAddressResVo queryAddress(UserQueryAddressReqVo reqVo) {
        UserQueryAddressResVo resVo = new UserQueryAddressResVo();
        TUserAddress userAddress = userAddressService.queryByUserId(reqVo.getUserId());
        if (Objects.nonNull(userAddress)) {
            resVo.build(userAddress);
        }
        return resVo;
    }

    /**
     * @Author: ZhaoZiYin
     * @Date: 2025/4/22
     * @Params: [reqVo]
     * @Return: UserUpdateAddressResVo
     * @Description: 用户保存收货地址
     */
    @Transactional(rollbackFor = Exception.class)
    public UserSaveAddressResVo saveAddress(UserSaveAddressReqVo reqVo) {
        log.info("用户保存收货地址-服务处理开始-入参={}", JSONObject.toJSONString(reqVo));
        TUserAddress userAddress = reqVo.build();
        userAddressService.deleteByUserId(reqVo.getUserId());
        userAddressService.save(userAddress);
        log.info("用户保存收货地址-服务处理成功");
        return null;
    }

}
